﻿using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;

namespace Northwind.V2.Domain
{

    public partial class NorthwindDbContext : DbContext
    {
        public NorthwindDbContext() : base("Northwind")
        {
            //Database.SetInitializer(new NorthwindInitializer());
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //配置Purchase与PurchaseItem的1：n关联关系、外键和级联删除
            modelBuilder.Entity<PurchaseItem>()
                .HasRequired(pi => pi.Purchase)
                .WithMany(p => p.PurchaseItems)
                .HasForeignKey(pi => pi.PurchaseID)
                .WillCascadeOnDelete(true);
            //配置Customer与Purchase的0..1：n关联关系、外键和级联删除
            modelBuilder.Entity<Purchase>()
                //配置关联关系
                .HasOptional(p => p.Customer)
                .WithMany(c => c.Purchases)
                //配置外键信息
                .HasForeignKey(p => p.CustomerID)
                //配置支持级联删除？
                .WillCascadeOnDelete(true);
            base.OnModelCreating(modelBuilder);
        }

        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<MedicalArticle> MedicalArticles { get; set; }
        public virtual DbSet<Product> Products { get; set; }
        public virtual DbSet<Purchase> Purchases { get; set; }
        public virtual DbSet<PurchaseItem> PurchaseItems { get; set; }
    }



    public class NorthwindInitializer : DropCreateDatabaseAlways<NorthwindDbContext>
    {
        /// <summary>
        /// 添加测试数据
        /// </summary>
        /// <param name="context"></param>
        protected override void Seed(NorthwindDbContext context)
        {
            //添加两篇媒体文章
            context.MedicalArticles.AddOrUpdate(new MedicalArticle() { Topic = "Influenza", Abstract = "<this is the abstract...>" });
            context.MedicalArticles.AddOrUpdate(new MedicalArticle() { Topic = "Diabetes", Abstract = "<this is the abstract...>" });
            //添加一个产品信息
            context.Products.AddOrUpdate(new Product { Description = "Widget", Discontinued = false, LastSale = new DateTime(2007, 1, 1) });
            //添加tom客户及相关购物信息
            var tom = new Customer() { Name = "Tom" };
            tom.Purchases.Add(new Purchase { Date = new DateTime(2006, 1, 1), Description = "Bike", Price = 500m });
            var purchase = new Purchase { Date = new DateTime(2006, 1, 2), Description = "Holiday", Price = 2000m };
            tom.Purchases.Add(purchase);
            purchase.PurchaseItems.Add(new PurchaseItem() { Detail = "Flight", Price = 1500m });
            purchase.PurchaseItems.Add(new PurchaseItem() { Detail = "Accommodation", Price = 500m });
            purchase.PurchaseItems.Add(new PurchaseItem() { Detail = "Camera", Price = 400m });
            context.Customers.AddOrUpdate(tom);
            //添加dick客户信息
            var dick = new Customer { Name = "Dick" };
            dick.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 1), Description = "Bike", Price = 600 });
            dick.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 2), Description = "Phone", Price = 300m });
            context.Customers.AddOrUpdate(dick);
            //添加harry客户信息
            var harry = new Customer { Name = "Harry" };
            harry.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 1), Description = "hat", Price = 50m });
            context.Customers.AddOrUpdate(harry);
            //添加marry客户信息
            var marry = new Customer { Name = "Marry" };
            marry.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 2), Description = "Car", Price = 15000 });
            marry.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 3), Description = "Boat", Price = 30000m });
            marry.Purchases.Add(new Purchase { Date = new DateTime(2007, 1, 4), Description = "Camera", Price = 1200m });
            context.Customers.AddOrUpdate(marry);
            //添加jay客户信息
            var jay = new Customer { Name = "Jay" };
            context.Customers.AddOrUpdate(jay);
            //保存到数据库
            context.SaveChanges();

        }
    }
}
